import { createStore } from "vuex"


export default createStore({
  state: {
    value: [
      {
        date: "10/12/2017",
        id: 1,
        description: "1",
        total: 1
      },
      {
        date: "10/12/2019",
        id: 2,
        description: "1",
        total: 1
      },
      {
        date: "10/12/2019",
        id: 3,
        description: "1",
        total: 1
      },
      {
        date: "10/12/2019",
        id: 4,
        description: "1",
        total: 1
      },
      {
        date: "10/12/2019",
        id: 5,
        description: "1",
        total: 1
      },
      {
        date: "10/12/2019",
        id: 6,
        description: "1",
        total: 1
      }    
    ].sort((a, b)=>{
      let dateA = new Date(a.date)
      let dateB = new Date(b.date)
      if(dateA>dateB){
        return -1
      }else{
        return 1
      }
    }),
    show: false,
    min:0,
    on:1,
    onIndex: -1
  },
  mutations:{
    add(state, item){
      state.value.push(item)
      state.value.sort((a, b)=>{
        let dateA = new Date(a.date)
        let dateB = new Date(b.date)
        if(dateA>dateB){
          return -1
        }else{
          return 1
        }
      })
    },
    delete(state, item){
      let index = state.value.indexOf(item)
      state.value.splice(index,1)
    },
    keep(state, itemAll){
      let item = itemAll.item
      let newItem = itemAll.items
      let index = state.value.indexOf(item)
      state.value[index] = newItem
      state.value.sort((a, b)=>{
        let dateA = new Date(a.date)
        let dateB = new Date(b.date)
        if(dateA>dateB){
          return -1
        }else{
          return 1
        }
      })
    }
  }
})
